package com.yanqu.road.dao.impl.monopolymarket.fortress;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.fortress.MonopolyMarketFortressUnionData;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/10/20 21:36
 */
public class MonopolyMarketFortressUnionDataDaoImpl extends TempDataObjectDao<MonopolyMarketFortressUnionData> {
    public static String getMonopolyMarketFortressUnionDataSql = "select * from `t_c_monopolymarket_fortress_union` where `groupId`=? and `weekId`=? and `isDelete` = false";
    public List<MonopolyMarketFortressUnionData> getMonopolyMarketFortressUnionDataList(long groupId,int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<MonopolyMarketFortressUnionData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketFortressUnionDataSql);
            int i = 1;
            call.setLong(i++, groupId);
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateMonopolyMarketFortressUnionDataSql = "update `t_c_monopolymarket_fortress_union` set unionBaseInfo=?,memberId=?,isDelete=? where groupId=? and weekId=? and unionId=?";
    @Override
    protected boolean updateData(MonopolyMarketFortressUnionData monopolyMarketFortressUnionData) {
        monopolyMarketFortressUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketFortressUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketFortressUnionData.getMemberIdList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketFortressUnionData.getIsDelete()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressUnionData.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressUnionData.getUnionId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketFortressUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketFortressUnionDataSql = "insert into `t_c_monopolymarket_fortress_union`(groupId,weekId,unionId,unionBaseInfo,memberId,isDelete) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketFortressUnionData monopolyMarketFortressUnionData) {
        monopolyMarketFortressUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressUnionData.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketFortressUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketFortressUnionData.getMemberIdList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketFortressUnionData.getIsDelete()));
        if (getDb().executeNoneQuery(insertMonopolyMarketFortressUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketFortressUnionData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketFortressUnionData monopolyMarketFortressUnionData = new MonopolyMarketFortressUnionData();
        monopolyMarketFortressUnionData.setGroupId(rs.getLong("groupId"));
        monopolyMarketFortressUnionData.setWeekId(rs.getInt("weekId"));
        monopolyMarketFortressUnionData.setUnionId(rs.getString("unionId"));
        monopolyMarketFortressUnionData.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        monopolyMarketFortressUnionData.setMemberIdList(JSON.parseObject(rs.getString("memberId"), new TypeReference<List<Long>>() {}));
        monopolyMarketFortressUnionData.setIsDelete(rs.getBoolean("isDelete"));

        return monopolyMarketFortressUnionData;
    }
}
